<%-- 
    Document   : queryInfo
    Created on : 2017-3-24, 10:15:04
    Author     : 52461
--%>

<%@page contentType="text/html" pageEncoding="UTF-8"%>
<%@include file="../link.jsp"%>
<!DOCTYPE html>
<html>
    <head>
        <meta http-equiv="Content-Type" content="text/html; charset=UTF-8">
        <title>查询统计</title>
        <style>
            .form-control{
                width: 100px;
            }
        </style>
    </head>
    <body ng-app="queryApp" ng-controller="mainCtrl" style="overflow: hidden;margin-top: 5px;">
        <div class="col-lg-12">
            <div class="panel panel-default" style="overflow: hidden">

                <div class="panel-heading head-width"><span style="float: left;line-height: 30px;font-size: 16px; font-weight: bold;margin-right: 10px;">查询统计</span>
                    <div class="form-inline">
                        <div class="form-group">
                            <label for="patName">名字/病历号:</label>
                            <input type="text" ng-model="searchContent" style="width:120px" class="form-control" id="patName" >
                        </div>
                        <div class="form-group">
                            <label for="startTime">开始时间:</label>
                            <input type="text" ng-model="startDate" style="width:120px" class="form-control" id="start_date" >
                        </div>
                        <div class="form-group">
                            <label for="startTime">结束时间:</label>
                            <input type="text" ng-model="endDate" style="width:120px" class="form-control" id="end_date" >
                        </div>
                        <div class="form-group">
                            <label for="startTime">检验项目:</label>
                            <input type="text" ng-model="proName" style="width:120px" class="form-control" id="startTime">
                        </div>
                        <div class="form-group">
                            <label for="startTime">设备:</label>
                            <select ng-model="device" style="width:120px" class="form-control" id="startTime">
                                <option ng-repeat="de in des" value="{{de.DEVICEMODEL}}">{{de.DEVICEMODEL}}</option>
                            </select>
                        </div>
                        <button ng-click="queryInfo()" class="btn btn-success btn-sm"><span class="fa fa-search"></span>&nbsp;&nbsp;&nbsp;查询</button>
                        <button ng-click="printResult()" class="btn btn-success btn-sm"><span class="fa fa-print"></span>&nbsp;&nbsp;&nbsp;打印</button>
                    </div>
                    <!--                    <div>
                                            <input ng-model="searchContent" ng-model="name2" placeholder="名字/病历号" style="width:320px; float: left;margin-left: 20px" class="form-control input-sm" />
                                            <button ng-click="getList()" style="float: left" class="btn btn-success btn-sm"><span class="fa fa-search"></span>&nbsp;&nbsp;搜索</button>
                    
                                            <button ng-click="addUser()" style="margin-left: 30px;float: left" class="btn btn-info btn-sm"><span class="fa fa-send"></span>&nbsp;&nbsp;添加</button>
                                        </div>-->

                    <div style="clear: both"></div>
                </div>

                <table  style="width:100%">


                    <tr>
                        <td  class="main-left" valign="top" style="overflow: auto">
                            <div class=" main main-left" style="overflow: auto;width: 100%"  >
                                <table class="table table-condensed" style="width:1500px">
                                    <tr>
                                        <th style="width:80px;">病例号</th>
                                        <th>姓名</th>
                                        <th>性别</th>
                                        <th>年龄</th>
                                        <th>检验时间</th>
                                        <th>仪器类型</th>
                                        <th>状态</th>
                                        <th>样本号</th>
                                        <th>类型</th>
                                        <th>科室</th>
                                        <th>审核人</th>
                                        <th>打印时间</th>


                                        <th>检验医生</th>
                                    </tr>
                                    <tr ng-repeat="list in lists"  id="do" ng-click="getResult(list, $event)">
                                        <td style="width:80px;">{{list.PATIENTID}}</td>
                                        <td>{{list.PATIENTNAME}}</td>
                                        <td >{{list.SEX}}</td>
                                        <td >{{list.AGE}}</td>
                                        <td width="130px" >{{list.RECEIVETIME}}</td>
                                        <td>{{list.DEVICEID}}</td>
                                        <td>{{list.RESULTSTATUS}}</td>
                                        <td>{{list.SAMPLENO}}</td>
                                        <td >{{list.STAYHOSPITALMODE}}</td>
                                        <td >{{list.SECTION}}</td>
                                        <td>{{list.CHECKOPERATOR}}</td>
                                        <td>{{list.PRINTTIME}}</td>


                                        <td >{{list.JYYS}}</td>
                                    </tr>
                                </table>
                            </div>

                        </td>
                        <td style="border-left: 1px silver solid">
                            <div class="col-lg-12 main" style="overflow: auto">
                                <table class="table table-condensed">
                                    <tr id="tr1">
                                        <th>序号</th>
                                        <th>代码</th>
                                        <th>检验项目</th>
                                        <th></th>
                                        <th>结果</th>
                                        <th>单位</th>
                                        <th>范围</th>
                                    </tr>
                                    <tr ng-repeat="result in results| filter:{RESULTFLAG:'0'}"  id="dp" ng-class="{true: 'danger', false: 'warning'}[result.isActive]">
                                        <td>{{$index + 1}}</td>
                                        <td >{{result.TESTID}}</td>
                                        <td>{{result.CHINESENAME}} </td>
                                        <td style="width:10px;">{{result.HL_STATUS}}</td>
                                        <td>{{result.TESTRESULT}}</td>
                                        <td>{{result.UNIT}}</td>
                                        <td>{{result.fanwei}}</td>
                                    </tr>
                                </table>
                            </div>
                        </td>



                    </tr>

                    </tbody>
                </table>
                <div class="panel-heading" >
                    <div style="float:left;font-size: 16px;"><span  >共</span><span style="font-weight: bolder" ng-bind="data.bigTotalItems"></span><span style="font-size: 16px">项</span></div>
                    <div style="float:left;margin-left: 20px;" ><pagination ng-change="pageChanged()" previous-text="上一页" next-text="下一页" first-text="第一页" last-text="最后一页" ng-model="data.currentPage" items-per-page="data.pageSize" total-items="data.bigTotalItems" ng-model="data.bigCurrentPage" max-size="data.maxSize" class="pagination-sm" boundary-links="true" rotate="false" num-pages="numPages"></pagination>
                    </div>
                    <div style="clear: both">
                    </div>
                </div>

            </div>

        </div>
    </body>
</html>
<script>
    angular.module("queryApp", ['baseModule', 'ui.bootstrap'])

            .controller("mainCtrl", function ($scope, base) {
                $scope.data = {currentPage: 1};
                $scope.setPage = function (pageNo) {
                    $scope.data.currentPage = pageNo;
                };
                $scope.pageChanged = function () {
                    $scope.queryInfo();
                };
                $scope.data.maxSize = 5;
                $scope.data.bigTotalItems = 20;
                $scope.data.bigCurrentPage = 1;
                $scope.data.pageSize = 20;
                $scope.queryInfo = function () {
                    base.post("select/queryList", {pageSize: $scope.data.pageSize, currentPage: $scope.data.currentPage, searchContent: $scope.searchContent,startDate:$scope.startdate,endDate:$scope.endDate,proName:$scope.proName,device:$scope.device}).get(function (data) {

                        $scope.changeZt(data.list);

                        $scope.lists = data.list;

                        $scope.data.bigTotalItems = data.totalRow;
                    });
                }
                $scope.changeZt = function (data) {
                    for (var i = 0; i < data.length; i++) {
                        if (data[i].RECEIVETIME) {

                            data[i].RECEIVETIME = data[i].RECEIVETIME.split(" ")[0];
                        }
                        if (data[i].RESULTSTATUS == "0") {
                            data[i].RESULTSTATUS = "无结果";
                        }
                        if (data[i].RESULTSTATUS == "1" || data[i].RESULTSTATUS == null) {
                            data[i].RESULTSTATUS = "未审核";
                        }
                        if (data[i].RESULTSTATUS == "2") {
                            data[i].RESULTSTATUS = "已审核"
                        }
                        if (data[i].RESULTSTATUS == "3") {
                            data[i].RESULTSTATUS = "已打印"
                        }
                    }
                }
                $scope.getResult = function (item, event) { //获取项目

                    $(event.target).parent().siblings().removeClass("bg-success");
                    $(event.target).parent().addClass("bg-success");
                    $(event.target).removeClass("bg-success");
                    $scope.info = item;
                    $scope.UUID = item.UUID;
                    $scope.ybh = item.SAMPLENO;
                    $scope.samplepk = item.PK;
                    $scope.sampleNo = item.SAMPLENO;
                    $scope.checkpt = item.RESULTSTATUS;
                    $scope.info = item;
                    $scope.brpk = item.brpk;
                    base.post("host/getResult", {UUID: item.UUID}).get(function (data) {
                        if (data) {
                            //$scope.handerImage(data);
                            $scope.checkResult(data);
                            $scope.sortResult(data);
                            //console.table($scope.results);
                        }
                    })
                }
                $scope.handerImage = function (data) {
                    WBC = [], RBC = [], PLT = [], BASO = [], DIFF = [], SRET = [], SWDF = [], SWNR = [];
                    for (var i = 0; i < data.length; i++) {
                        //data[i].TESTRESULT
                        if (data[i].TESTRESULT == 'NaN') {
                            data[i].TESTRESULT = "";
                        }

                        if (data[i].RESULTFLAG == "4") {
                            $scope.moreImage.push("data:image/jpg;base64," + data[i].TESTRESULT);
                        }
                        //console.log(data[i].TESTID);
                        if (data[i].TESTID == 'image#WBC') {

                            $scope.sh_wbc = true;
                            if (data[i].RESULTFLAG == '2') {
                                WBC = data[i].TESTRESULT;
                            } else if (data[i].RESULTFLAG == '3') {
                                var wbcs = data[i].TESTRESULT.split(",");
                                for (s in wbcs) {
                                    WBC.push([parseFloat($.trim(wbcs[s]).split(" ")[0]), parseFloat($.trim(wbcs[s]).split(" ")[1])]);
                                }
                            } else {
                                WBC = data[i].TESTRESULT.split(" ");

                            }


                        } else if (data[i].TESTID == 'image#RBC') {
                            $scope.sh_rbc = true;
                            if (data[i].RESULTFLAG == '2') {
                                RBC = data[i].TESTRESULT;
                            } else {
                                RBC = data[i].TESTRESULT.split(" ");
                            }

                        } else if (data[i].TESTID == 'image#PLT') {
                            $scope.sh_plt = true;
                            if (data[i].RESULTFLAG == '2') {
                                PLT = data[i].TESTRESULT;
                            } else {
                                PLT = data[i].TESTRESULT.split(" ");
                            }

                        } else if (data[i].TESTID == 'image#DIFF') {
                            $scope.sh_diff = true;
                            if (data[i].RESULTFLAG == '2') {
                                DIFF = data[i].TESTRESULT;
                            }
                            if (data[i].RESULTFLAG == '3') {
                                var diffs = data[i].TESTRESULT.split(",");
                                for (s in diffs) {
                                    DIFF.push([parseFloat($.trim(diffs[s]).split(" ")[0]), parseFloat($.trim(diffs[s]).split(" ")[1])]);
                                }
                            } else {
                                DIFF = data[i].TESTRESULT.split(" ");
                            }

                        } else if (data[i].TESTID == 'image#BASO') {
                            $scope.sh_baso = true;
                            if (data[i].RESULTFLAG == '3') {
                                var basos = data[i].TESTRESULT.split(",");
                                for (s in basos) {
                                    BASO.push([parseFloat($.trim(basos[s]).split(" ")[0]), parseFloat($.trim(basos[s]).split(" ")[1])]);
                                }
                            } else if (data[i].RESULTFLAG == '2') {
                                BASO = data[i].TESTRESULT;
                            }

                        } else if (data[i].TESTID == 'image#SWDF') {
                            $scope.sh_swdf = true;
                            if (data[i].RESULTFLAG == '2') {
                                SWDF = data[i].TESTRESULT;
                            }

                        } else if (data[i].TESTID == 'image#SWNR') {
                            $scope.sh_swnr = true;
                            if (data[i].RESULTFLAG == '2') {
                                SWNR = data[i].TESTRESULT;
                            }

                        } else if (data[i].TESTID == 'image#SRET') {
                            $scope.sh_sret = true;
                            if (data[i].RESULTFLAG == '2') {
                                SRET = data[i].TESTRESULT;
                            }

                        }
                    }
                }
                $scope.checkResult = function (data) {
                    for(var i=0;i<data.length;i++)
                    if (!isNaN(data[i].TESTRESULT)) {
                        if (data[i].MREFLO0) {
                            var lower = data[i].MREFLO0;
                            var hight = data[i].MREFHI0;
                            var lh = lower + "-" + hight;
                            data[i].fanwei = lh;
                            // console.log(data[i].fanwei +""+data[i].)
                            console.log((parseFloat(data[i].TESTRESULT) < parseFloat(data[i].MREFLO0)) + " " + parseFloat(data[i].TESTRESULT) + " " + data[i].MREFLO0)
                            if (parseFloat(data[i].TESTRESULT) < parseFloat(data[i].MREFLO0)) {
                                //console.log(data[i].TESTRESULT + "   " + data[i].MREFLO0);
                                data[i].isActive = true;
                                data[i].HL_STATUS = "↓";
                            } else if (parseFloat(data[i].TESTRESULT) > parseFloat(data[i].MREFHI0)) {
                                data[i].isActive = false;
                                data[i].HL_STATUS = "↑";
                            }
                            var k = 2;
                            if (data[i].Decimalplace) {
                                k = data[i].Decimalplace;

                            }
                            data[i].TESTRESULT = parseFloat(data[i].TESTRESULT).toFixed(k);
                        }
                    }
                }
                $scope.sortResult = function (data) {
                    for (var i in data) {
                        if (!data[i].index) {
                            data[i].index = 999;
                        }
                    }
                    var d1 = [], d2 = [];
                    var dlist = [];
                    for (var s = 0; s < data.length; s++) {
                        if (data[s].MEASURETIME == data[0].MEASURETIME) {
                            d1.push(data[s]);
                        } else {
                            d2.push(data[s]);
                        }
                    }

                    d1.sort(function (a, b) {
                        return a.index - b.index;
                    });
                    dlist = d1;
                    //console.table(dlist);
                    if (d2.length > 0) {
                        d2.sort(function (a, b) {
                            return a.index - b.index;
                        });
                        dlist = d1.concat(d2);
                    }



                    //  console.table(dlist);
                    $scope.results = dlist;
                }
                var checkRange = function () {
                    console.log($scope.info);
                    if ($scope.info && $scope.results && $scope.info.AGE && $scope.info.SEX) {
                        console.log("1111111111");
                        var data = $scope.results;
                        for (var i in data) {
                            if (data[i].ranges) {
                                console.log(data[i].ranges);
                                var range = JSON.parse(unescape(data[i].ranges));
                                var item_range = checkAge_sex_range(data[i], range);

                                if (item_range) {
                                    var lower = item_range.min_val;
                                    var hight = item_range.max_val;
                                    var lh = lower + "-" + hight;
                                    data[i].fanwei = lh;

                                    //console.log((parseFloat(data[i].TESTRESULT) < parseFloat(data[i].MREFLO0)) + " " + parseFloat(data[i].TESTRESULT) + " " + data[i].MREFLO0)
                                    if (parseFloat(data[i].TESTRESULT) < parseFloat(lower)) {
                                        //console.log(data[i].TESTRESULT + "   " + data[i].MREFLO0);
                                        data[i].isActive = true;
                                        data[i].HL_STATUS = "↓";
                                    } else if (parseFloat(data[i].TESTRESULT) > parseFloat(hight)) {
                                        data[i].isActive = false;
                                        data[i].HL_STATUS = "↑";

                                    } else {
                                        data[i].isActive = null;
                                        data[i].HL_STATUS = "";
                                    }
                                }
                            }
                        }
                    }
                }

                var checkAge_sex_range = function (item, range) {
                    var age = parseInt($scope.info.AGE);
                    //var item_range={};
                    for (var i in range) {
                        console.log("222333");
                        if (range[i].sex == $scope.info.SEX) {

                            if (range[i].max_age >= age && age >= range[i].min_age) {
                                return range[i];
                                break;
                            }
                        }
                    }
                }
                
                $scope.printResult = function () {  //打印
                    if ($scope.checkpt == "已审核" || $scope.checkpt == "已打印") {
                        var date = new Date().Format("yyyy-MM-dd hh:mm:ss");
                        base.post("host/printReport", {pk: $scope.samplepk, time: date, device: $scope.info.DEVICEID}).get(function (data) {

                            if (data == "false") {
                                steveAl("打印出错", 1000);
                                // $scope.getList();

                            } else {

                                //$scope.lists[$scope.sample_index].RESULTSTATUS = "已打印";

                                try {
                                    if (data.length) {
                                        localStorage.reprotConfig = JSON.stringify(angular.fromJson(unescape(data)));
                                    }
                                } catch (e) {

                                }

                                localStorage.results = JSON.stringify($scope.results);
                                localStorage.sampleNo = $scope.sampleNo;
                                localStorage.date = date;
                                localStorage.info = JSON.stringify($scope.info);
                                var flagRpt = true;
                                var count = 0;
                                if ($scope.info.DEVICEID == 'AVE-752') {
                                    window.open("fomatRpt_1.jsp");
                                } else {

                                    for (i in $scope.results) {
                                        var test_id = $scope.results[i].TESTID;
                                        if (test_id && test_id.indexOf('image') > -1) {
                                            count = count + 1;
                                        }
                                    }
                                    if (count > 3) {
                                        window.open("printReport-1-4image.jsp");

                                    } else {
                                        window.open("printReport-1-4image.jsp");
                                    }
                                }
                            }
                        })
                    } else {
                        steveAl("未审核不能进行打印!", 1000);
                    }
                }
                $scope.getDeviceType = function () {
                    base.post("host/getType").get(function (data) {
                        $scope.des = data;

                    });
                }

                $scope.queryInfo();
                 $scope.getDeviceType();
                
                
                
                 layui.use('laydate', function () {
                    var laydate = layui.laydate;

                    var start = {
//    min: laydate.now(),
                        max: '2099-06-16 23:59:59'
                        , istoday: false
                        , choose: function (datas) {
                            $scope.startdate = datas;
                            end.min = datas; //开始日选好后，重置结束日的最小日期
                            end.start = datas; //将结束日的初始值设定为开始日
                        }
                    };

                    var end = {
                        min: laydate.now()
                        , max: '2099-06-16 23:59:59'
                        , istoday: false
                        , choose: function (datas) {
                            $scope.endDate = datas;
                            start.max = datas; //结束日选好后，重置开始日的最大日期
                        }
                    };

                    document.getElementById('start_date').onclick = function () {
                        start.elem = this;
                        laydate(start);
                    }
                    document.getElementById('end_date').onclick = function () {
                        end.elem = this
                        laydate(end);
                    }

                });

            });





    function autoHeight() {
        var bHeight = $(window).height();
        var bWidth = $(window).width();
         console.log($(".head-width").height()+"------------------");
        $(".main").height(bHeight - $(".head-width").height()-75);
        $(".main-left").width(bWidth / 2 -200);


    }
    autoHeight();


    $(window).resize(function () {
        autoHeight();
    });
</script>
